using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Project : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sbuScript = new StringBuilder();
        sbuScript.AppendLine("<script language=\"javascript\" type=\"text/javascript\">");
        sbuScript.AppendLine("<!--");
        sbuScript.AppendLine("var win=null;");
        sbuScript.AppendLine("function NewWindow(mypage,myname,w,h,scroll,pos){");        
        sbuScript.AppendLine("if(pos==\"random\"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}");
        sbuScript.AppendLine("if(pos==\"center\"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}");
        sbuScript.AppendLine("else if((pos!=\"center\" && pos!=\"random\") || pos==null){LeftPosition=0;TopPosition=20}");
        sbuScript.AppendLine("settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';");
        sbuScript.AppendLine("win=window.open(mypage,myname,settings);}");


        sbuScript.AppendLine("function SetImage(varID)");
        sbuScript.AppendLine("{");
        sbuScript.AppendLine("document.forms[0].ctl00_Main_imaThumb.src = '../Handler.ashx?Size=S&PhotoID='+varID ;");
        sbuScript.AppendLine("}");

        sbuScript.AppendLine("function NewPhotoWindow(mypage,myname,w,h,scroll,pos)");
        sbuScript.AppendLine("{");
        sbuScript.AppendLine("if(document.forms[0].elements['" + hfiAlbumID.ClientID + "'].value == '' )");
        sbuScript.AppendLine("{");
        sbuScript.AppendLine("alert('Please choose an Album first.');");
        sbuScript.AppendLine("return false;");
        sbuScript.AppendLine("}");
        sbuScript.AppendLine("NewWindow(mypage,myname,w,h,scroll,pos);");
        sbuScript.AppendLine("}");


        sbuScript.AppendLine("// -->");
        sbuScript.AppendLine("</script>");      

        ClientScript.RegisterClientScriptBlock(this.GetType(),"script", sbuScript.ToString());

        butAlbumSelect.Attributes["OnClick"] = "NewWindow('projectalbum.aspx','Choose a Project Album','450','600','yes','center');return false;";
        butClearAlbum.Attributes["OnClick"] = "document.forms[0].elements['" + hfiAlbumID.ClientID + "'].value='';document.forms[0].elements['" + tboAlbum.ClientID + "'].value='';";

        butFileSelect.Attributes["OnClick"] = "NewWindow('projectfilelist.aspx','Choose a Project file','450','600','yes','center');return false;";
        butFileClear.Attributes["OnClick"] = "document.forms[0].elements['" + hfiFileID.ClientID + "'].value='';document.forms[0].elements['" + tboFile.ClientID + "'].value='';";

        imaThumb.Attributes["OnClick"] = "NewPhotoWindow('ProjectPhoto.aspx?AlbumID=' + document.forms[0].elements['" + hfiAlbumID.ClientID + "'].value,'Choose a Project Album','450','600','yes','center');return false;";

        // if this is an edit then the last page will send the project id 
        // and we need to set the select statment for the sql data source
        if (!IsPostBack)
        {
            // fill the drop down lists
            ddlCAts.DataSource = ProjectManager.GetProjectCategories();
            ddlCAts.DataBind();

            ddlStatus.DataSource = ProjectManager.GetProjectStatuses();
            ddlStatus.DataBind();

            if (Request.QueryString["id"] != null)
            {
                // seeing that this is an update not an insert we are going to do the select and set the fields now.
                hfiProjectID.Value = Request.QueryString["id"];

                // get the project data
                Project proCurrent = ProjectManager.GetProject(Convert.ToInt32(Request.QueryString["id"]));
                if (proCurrent != null)
                {
                    // set the values
                    tboTitle.Text = proCurrent.Title;
                    ftbLongDesc.Text = proCurrent.LongDesc;
                    ftbShortDesc.Text = proCurrent.ShortDesc;
                    tboURL.Text = proCurrent.URL;
                    tboDisplay.Text = proCurrent.Display;

                    // so that if there is an error (like a deleted cat or status) 
                    try
                    {


                        ddlCAts.ClearSelection();
                        ddlCAts.Items.FindByValue(proCurrent.CatID.ToString()).Selected = true;
                    }
                    catch
                    {

                    }

                    try
                    {
                        ddlStatus.ClearSelection();
                        ddlStatus.Items.FindByValue(proCurrent.StatusID.ToString()).Selected = true;
                    }
                    catch
                    {

                    }

                   

                    //hfiWhitePaper.Value = sdrProjectInfo["PRO_WHITE_PAPER_ID"].ToString();
                    hfiFileID.Value = proCurrent.FileID.ToString();
                    tboFile.Value = proCurrent.FileName;
                    hfiThumbID.Value = proCurrent.PhotoID.ToString();

                    hfiAlbumID.Value = proCurrent.AlbumID.ToString();
                    tboAlbum.Value = proCurrent.AlbumTitle;


                    if (proCurrent.AlbumID < 0)
                    {
                        imaThumb.ImageUrl = "../Handler.ashx?Size=S&PhotoID=";
                        imaThumb.AlternateText = "No Image";
                    }
                    else
                    {
                        if (proCurrent.PhotoID < 0)
                        {
                            imaThumb.ImageUrl = "../Handler.ashx?Size=S&AlbumID=" + proCurrent.AlbumID.ToString();
                        }
                        else
                        {
                            imaThumb.ImageUrl = "../Handler.ashx?Size=S&PhotoID=" + proCurrent.PhotoID.ToString();
                        }

                        imaThumb.AlternateText = "Thumbnail";// of Photo Number " + sdrProjectInfo["PRO_THUMB_ID"].ToString();
                    }
                }// end of project null check

            }// end of the if that checks for an id

        }// end of the postback check

    }// end of the page load method
    
    protected void butSave_Click(object sender, EventArgs e)
    {
        Project proCurrent = new Project();

        // insert of update as needed
        if (hfiProjectID.Value != null && hfiProjectID.Value != "")
        {
            // update 
            proCurrent.AlbumID = Convert.ToInt32(((hfiAlbumID.Value.Equals("")) ? "-1" : hfiAlbumID.Value));
            proCurrent.CatID = Convert.ToInt32(ddlCAts.SelectedValue);
            proCurrent.Display = tboDisplay.Text;
            proCurrent.FileID = Convert.ToInt32(((hfiFileID.Value.Equals("")) ? "-1" : hfiFileID.Value));
            proCurrent.ID = Convert.ToInt32(hfiProjectID.Value);
            proCurrent.LongDesc = ftbLongDesc.Text;
            proCurrent.ShortDesc = ftbShortDesc.Text;
            proCurrent.StatusID = Convert.ToInt32(ddlStatus.SelectedValue);
            proCurrent.Title = tboTitle.Text;
            proCurrent.URL = tboURL.Text;
            proCurrent.WhitePaperID = Convert.ToInt32(((hfiWhitePaper.Value.Equals("")) ? "-1" : hfiWhitePaper.Value));

            ProjectManager.UpdateProject(proCurrent);

        }// end of the update section 
        else
        {
            // insert
            proCurrent.AlbumID = Convert.ToInt32(((hfiAlbumID.Value.Equals("")) ? "-1" : hfiAlbumID.Value));
            proCurrent.CatID = Convert.ToInt32(ddlCAts.SelectedValue);
            proCurrent.Display = tboDisplay.Text;
            proCurrent.FileID = Convert.ToInt32(((hfiFileID.Value.Equals("")) ? "-1" : hfiFileID.Value));
            proCurrent.LongDesc = ftbLongDesc.Text;
            proCurrent.ShortDesc = ftbShortDesc.Text;
            proCurrent.StatusID = Convert.ToInt32(ddlStatus.SelectedValue);
            proCurrent.Title = tboTitle.Text;
            proCurrent.URL = tboURL.Text;
            proCurrent.WhitePaperID = Convert.ToInt32(((hfiWhitePaper.Value.Equals("")) ? "-1" : hfiWhitePaper.Value));

            ProjectManager.InsertProject(proCurrent);
        }// end of the insert section
        
        // end back to the projects list page
        Response.Redirect("projects.aspx");

    }// end of the butSave_Click method


    protected void ibuThumb_Click(object sender, ImageClickEventArgs e)
    {
       
    }// end of the ibuThumb_Click method
    
    protected void butWPSelect_Click(object sender, EventArgs e)
    {

    }// end of the butWPSelect_Click method
   
    protected void butCats_Click(object sender, EventArgs e)
    {
        Response.Redirect("projectcategories.aspx");
    }// end of the butCats_Click method
   
    protected void butStatuses_Click(object sender, EventArgs e)
    {
        Response.Redirect("projectstatuses.aspx");
    }// end of the butStatuses_Click method

}
